<template>
  <div class="article">
    <!-- 导航栏 -->
    <van-nav-bar title="文章详情" left-arrow @click-left="$router.back()" />

    <div class="article-wrap">
      <h3 class="title">{{ info.title }}</h3>
      <div class="autInfo">
        <van-image fit="cover" class="photo" round :src="info.aut_photo" />
        <!-- 发布者信息 -->
        <div class="info">
          <div>{{ info.aut_name }}</div>
          <div class="time">{{ info.pubdate | relvTime }}</div>
        </div>
        <div v-if="$store.state.userInfo.id != info.aut_id">
          <van-button
            @click="doFollow(true)"
            v-if="!info.is_followed"
            round
            type="info"
            icon="plus"
            >关注</van-button
          >
          <van-button @click="doFollow(false)" v-else round type="danger"
            >取关</van-button
          >
        </div>
      </div>
      <!-- 内容区域 -->
      <div class="content" v-html="info.content">我是内容</div>
      <!-- 分割线 -->
      <van-divider>end</van-divider>
      <!-- 点赞区域 -->
      <div class="btn-warp">
        <van-button
          v-if="info.attitude == -1"
          @click="zan(true)"
          round
          icon="good-job-o"
          plain
          type="info"
          >点赞</van-button
        >
        <van-button
          v-if="info.attitude == 1"
          @click="zan(false)"
          round
          icon="good-job"
          plain
          type="info"
          >取消点赞</van-button
        >
        <div v-if="info.attitude == -1" class="btn"></div>
        <van-button
          @click="unlike(true)"
          v-if="info.attitude == -1"
          round
          icon="delete-o"
          plain
          type="danger"
          >不喜欢</van-button
        >
        <van-button
          @click="unlike(false)"
          v-if="info.attitude == 0"
          round
          icon="delete"
          plain
          type="danger"
          >取消</van-button
        >
      </div>
    </div>

    <!-- 评论区 -->
    <comment />
  </div>
</template>

<script>
import {
  articleInfoAPI,
  unfollowUserAPI,
  followUserAPI,
  zanAPI,
  unZanAPI,
  unlikeAPI,
  cancelLikeAPI
} from '@/api'
import comment from '@/views/article/comment.vue'
export default {
  components: { comment },
  name: 'Article',
  data () {
    return {
      info: {}
    }
  },
  methods: {
    // 关注事件
    async doFollow (status) {
      // 把要修改的状态赋值给数据
      this.info.is_followed = status

      try {
        // 需要发请求给服务器去做更新
        if (status) {
          // 为true代表要关注
          await followUserAPI({
            target: this.info.aut_id // 用户的id
          })
          this.$toast.success('关注成功')
        } else {
          // 取消关注
          await unfollowUserAPI(this.info.aut_id)
        }
        this.$toast.fail('取关成功')
      } catch {
        this.$toast.fail('操作失败!请与管理员联系')
      }
    },
    // 点赞事件
    async zan (status) {
      if (status) {
        // 点赞
        await zanAPI({
          target: this.info.art_id
        })
        this.info.attitude = 1
      } else {
        // 取消点赞
        await unZanAPI(this.info.art_id)
        this.info.attitude = -1
      }
    },
    // 不喜欢事件
    async unlike (status) {
      if (status) {
        // 不喜欢
        await unlikeAPI({
          target: this.info.art_id
        })
        this.info.attitude = 0
      } else {
        // 取消不喜欢
        await cancelLikeAPI(this.info.art_id)
        this.info.attitude = -1
      }
    }
  },
  // 获取新闻详情
  async created () {
    const res = await articleInfoAPI(this.$route.query.id)
    // console.log(res)
    this.info = res.data.data
  }
}
</script>

<style lang="less" scoped>
.article {
  .article-wrap {
    margin: 0 15px;
    .title {
      font-size: 24px;
      margin: 10px 0;
    }
    .autInfo {
      display: flex;
      align-items: center;
      margin-bottom: 10px;
      .photo {
        width: 60px;
        height: 60px;
      }
      .info {
        padding-left: 10px;
        flex: 1;
        .time {
          color: gray;
        }
      }
    }

    .content {
      // 如果最后一个单词不够放了，就把整个单词换一行开始写
      word-wrap: break-word;
      //   保持原来样式，包括换行，空格。。。
      white-space: pre-wrap;
      // word-break: break-all; 以一行实在放不下了就换行可能会导致单词脱节
      overflow: hidden;
      ::v-deep {
        pre {
          code {
            display: block;
            width: 100%;
            overflow: auto;
            background-color: #0f141c;
            color: #fff;
          }
        }
        img {
          width: 80%;
        }
      }
    }
    .btn-warp {
      display: flex;
      justify-content: center;
      .btn {
        width: 60px;
      }
    }
  }
}
</style>

Commont
